package person.bing.fbcloud.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: mengfanbing
 * @date: 2020/9/9 10:57
 */
public class Q39 {
    public static void main(String[] args) {
        int[] candidates = {2,3,6,7};
        int target = 10;
        combinationSum(candidates,target);
        System.out.println(ans);

    }
    private static List<List<Integer>> ans = new ArrayList<>();
    private static int[] candidates = null;
    public static List<List<Integer>> combinationSum(int[] candidates, int target) {
        Q39.candidates = candidates;
        util(target,new ArrayList<>(),0);
        return ans;
    }

    public static void util(int target,List<Integer> temp,int index) {
        if (target == 0){
            ans.add(new ArrayList<>(temp));
            return;
        }else if (target < 0){
            return;
        }else if(index == candidates.length){
            return;
        }else {
            util(target,temp,index+1);
            int indexValue = candidates[index];
            temp.add(indexValue);
            target -= indexValue;
            util(target,temp,index);
            temp.remove(Integer.valueOf(indexValue));
        }
    }
}
